﻿using System;
using System.Linq;
using System.ServiceModel;
using System.Threading.Tasks;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Input;
namespace WellDone
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class Login : Page
    {
        Popup dialogPopup;

        public Login()
        {
            this.InitializeComponent();
            Init();
        }

        private async Task<bool> getPinEnabled()
        {
            return await Globals.client.PINLoginEnabledAsync();
        }

        public void checkTimeClock()
        {
            var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
            string value = Convert.ToString(localSettings.Values[Globals.k_str_settings_use_timeClock_button]);
            if (value == "" || value.ToLower() == "false")
            {
                btnTimeClock.Visibility = Visibility.Collapsed;
                btnLogin.Margin = new Thickness(150, 344, 0, 0);
                Globals.timeclockbtnEnable = false;
            }
            else if (value.ToLower() == "true")
            {
                btnTimeClock.Visibility = Visibility.Visible;
                btnLogin.Margin = new Thickness(90, 347, 0, 0);
                btnTimeClock.Margin = new Thickness(185, 344, 0, 0);
                Globals.timeclockbtnEnable = true;
            }
            Globals.chkTimeclockEnable = false;
        }

        public async Task<bool> checkServiceAddress()
        {
            // Determine if an address has been set...
            var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
            Object value = localSettings.Values[Globals.k_str_settings_service_address];

            // kpw - 9/4/2017 - Determine whether or not links should be hidden
            Object hideLinks = localSettings.Values[Globals.k_str_settings_hide_login_links];
            if (hideLinks != null)
                if (hideLinks.ToString().ToUpper().Contains("TRUE") || hideLinks.ToString().ToUpper().Contains("YES"))
                    gridLinks.Visibility = Visibility.Collapsed;

            if (value != null)
            {
                string strClientAddress = value.ToString().Trim();
                if ((strClientAddress.Length > 0) && (strClientAddress != Globals.client.Endpoint.Address.Uri.ToString())) // Assume this is a good address
                {
                    Globals.client = new ServiceReferenceOpenRPOS.ServiceOpenRPOSClient();
                    try
                    {
                        /*
                        Globals.client.Endpoint.Address = new EndpointAddress(new Uri(strClientAddress),
                        Globals.client.Endpoint.Address.Identity, Globals.client.Endpoint.Address.Headers.ToArray());
                        //hbAccount.Visibility = Visibility.Collapsed;
                        await Globals.client.OpenAsync();
                         */
                        Globals.pinloginEnable = await getPinEnabled();
                    }
                    catch (Exception ex)
                    {
                        //hbAccount.Visibility = Visibility.Visible;
                        System.Diagnostics.Debug.WriteLine(ex.Message);
                    }
                }
                // if the address contains the word "Demo", show the "Create Account" link...
                //if (strClientAddress.ToLower().Contains("demo"))
                //    hbAccount.Visibility = Visibility.Visible;
            }
                /*
            else
                hbAccount.Visibility = Visibility.Visible;
            // Force login to demo service if needed
            if (hbAccount.Visibility == Visibility.Visible)
            {
                try
                {
                    Globals.client = new ServiceReferenceOpenRPOS.ServiceOpenRPOSClient();
                    Globals.client.Endpoint.Address = new EndpointAddress(new Uri(Globals.k_str_demo_service_address),
                    Globals.client.Endpoint.Address.Identity, Globals.client.Endpoint.Address.Headers.ToArray());
                    await Globals.client.OpenAsync();
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                }
            }*/

            return true;
        }


        //Show and Hide control for UserName,Password and Login Pin.
        private async void Init()
        {
            await checkServiceAddress();
            Globals.pinloginEnable = await getPinEnabled();
            checkTimeClock();
            if (Globals.pinloginEnable == true)
            {
                tblkUsername.Visibility = Visibility.Collapsed;
                tbUsername.Visibility = Visibility.Collapsed;

                tblkPassword.Visibility = Visibility.Collapsed;
                pbPassword.Visibility = Visibility.Collapsed;

                tblkPin.Visibility = Visibility.Visible;
                pbPin.Visibility = Visibility.Visible;

                btnLogin.Visibility = Visibility.Collapsed;
                btnTimeClock.Visibility=Visibility.Collapsed;

                btnUserLogin.Visibility = Visibility.Visible;

                NumericKeyPadControl.Visibility = Visibility.Visible;

                if (Globals.timeclockbtnEnable==true)
                {
                    chkTimeClock.Visibility = Visibility.Visible; 
                }
                else
                {
                    chkTimeClock.Visibility = Visibility.Collapsed; 
                }

                pbPin.Focus(Windows.UI.Xaml.FocusState.Pointer);
            }
            else if (Globals.pinloginEnable == false)
            {
                tblkUsername.Visibility = Visibility.Visible;
                tbUsername.Visibility = Visibility.Visible;

                tblkPassword.Visibility = Visibility.Visible;
                pbPassword.Visibility = Visibility.Visible;

                tblkPin.Visibility = Visibility.Collapsed;
                pbPin.Visibility = Visibility.Collapsed;

                NumericKeyPadControl.Visibility = Visibility.Collapsed;

                chkTimeClock.Visibility = Visibility.Collapsed;

                btnLogin.Visibility = Visibility.Visible;
                btnUserLogin.Visibility = Visibility.Collapsed;
                tbUsername.Focus(Windows.UI.Xaml.FocusState.Pointer);
            }
        }


        //code for login username and password
        private void btnUserLogin_Click(object sender, RoutedEventArgs e)
        {
            tblkUsername.Visibility = Visibility.Visible;
            tbUsername.Text = "";
            tbUsername.Visibility = Visibility.Visible;

            tblkPassword.Visibility = Visibility.Visible;
            pbPassword.Password = "";
            pbPassword.Visibility = Visibility.Visible;

            tblkPin.Visibility = Visibility.Collapsed;
            pbPin.Visibility = Visibility.Collapsed;

            NumericKeyPadControl.Visibility = Visibility.Collapsed;

            btnLogin.Visibility = Visibility.Visible;
            btnUserLogin.Visibility = Visibility.Collapsed;

            btnLoginPIN.Visibility = Visibility.Visible;

            chkTimeClock.Visibility = Visibility.Collapsed;
            
            checkTimeClock();

            tblkErrorMessage.Text = "";
            Globals.pinloginEnable = false;
        }

        //code for login PIN
        private void btnLoginPIN_Click(object sender, RoutedEventArgs e)
        {
            tblkUsername.Visibility = Visibility.Collapsed;
            tbUsername.Visibility = Visibility.Collapsed;

            tblkPassword.Visibility = Visibility.Collapsed;
            pbPassword.Visibility = Visibility.Collapsed;

            tblkPin.Visibility = Visibility.Visible;
            pbPin.Password = "";
            pbPin.Visibility = Visibility.Visible;

            NumericKeyPadControl.Visibility = Visibility.Visible;

            btnLogin.Visibility = Visibility.Collapsed;
            btnTimeClock.Visibility = Visibility.Collapsed;
            btnUserLogin.Visibility = Visibility.Visible;

            btnLoginPIN.Visibility = Visibility.Collapsed;

            if (Globals.timeclockbtnEnable == true)
            {
                chkTimeClock.Visibility = Visibility.Visible;
            }
            else
            {
                chkTimeClock.Visibility = Visibility.Collapsed;
            }

            tblkErrorMessage.Text = "";
            Globals.pinloginEnable = true;
        }


        private async void Loginmethod()
        {
            try
            {
                await checkServiceAddress();

                if (Globals.pinloginEnable == true)
                {
                    Globals.token = await Globals.client.PINLoginAsync(pbPin.Password);
                }
                else if (Globals.pinloginEnable == false)
                {
                    // kpw - 9/15/2017 - Updating login to include client information...
                    //Globals.token = await Globals.client.LoginAsync(tbUsername.Text, pbPassword.Password);
                    Globals.token = await Globals.client.LoginClientAsync(tbUsername.Text, pbPassword.Password, "Win8 POS");
                }

                // Attempt login...
                int currentLoginID = await Globals.client.GetLoginIDAsync(Globals.token);
                System.Collections.ObjectModel.ObservableCollection<WellDone.ServiceReferenceOpenRPOS.Login> loginCollection = await
                    Globals.client.GetLoginObjectsAsync(Globals.token, "ID=" + currentLoginID.ToString());
                Globals.currentLogin = loginCollection[0];
                // If the login was valid, go to the main display
                if (Globals.token > 0)
                {
                    // kpw - 9/3/2017 - Determine the tenant ID...
                    int currentTenantID = await Globals.client.GetTenantIDAsync(Globals.token);
                    if (currentTenantID != Globals.tenantID)
                    {
                        Globals.tenantID = currentTenantID;
                        Globals.initialized = false;
                    }
                    // Load menu item data...
                    if (!Globals.initialized)
                    {
                        Globals.MenuCategoryNoGroupRecords = await Globals.client.GetMenuCategoryObjectsAsync(Globals.token, "Active=1 and visible=1 and (MenuGroupID=0 or MenuGroupID is null) order by Precedence");
                        Globals.MenuCategoryRecords = await Globals.client.GetMenuCategoryObjectsAsync(Globals.token, "Active=1 and visible=1 order by Precedence");
                        Globals.MenuGroupRecords = await Globals.client.GetMenuGroupObjectsAsync(Globals.token, "Active=1 and visible=1 order by Precedence");
                        Globals.MenuItemRecords = await Globals.client.GetMenuItemObjectsAsync(Globals.token, "Active=1 and Enabled=1 order by FullName");
                        Globals.MenuItemOptionGroupRecords = await Globals.client.GetMenuItemOptionGroupObjectsAsync(Globals.token, "1=1 order by MenuItemID,Precedence");
                        Globals.MenuOptionRecords = await Globals.client.GetMenuOptionObjectsAsync(Globals.token, "");
                        Globals.OptionGroupRecords = await Globals.client.GetOptionGroupObjectsAsync(Globals.token, "Active=1");
                        Globals.LoginRecords = await Globals.client.GetLoginObjectsAsync(Globals.token, "");
                        Globals.PaymentTypeRecords = await Globals.client.GetPaymentTypeObjectsAsync(Globals.token, "1=1 order by ID");
                        Globals.TimePriceRecords = await Globals.client.GetTimePriceObjectsAsync(Globals.token, "1=1 order by ID, Interval");
                        Globals.processMercury = false;
                        Globals.processUSAePay = false;
                        Globals.processBlackline = false;

                        // Initialize the tax rate from configuration table
                        Globals.ConfigurationRecords = await Globals.client.GetConfigurationObjectsAsync(Globals.token, "KeyName='TaxRate'");
                        try
                        {
                            Globals.tax_rate = Convert.ToDouble(Globals.ConfigurationRecords[0].col_Value);
                        }
                        catch (Exception ex)
                        {
                            System.Diagnostics.Debug.WriteLine(ex.Message);
                        }

                        // Determine if the default view should be table layout...
                        Globals.ConfigurationRecords = await Globals.client.GetConfigurationObjectsAsync(Globals.token, "KeyName='DefaultTableView'");
                        if (Globals.ConfigurationRecords.Count > 0)
                        {
                            Globals.defaultTableView = Globals.ConfigurationRecords[0].col_Value.ToLower().Trim()=="true";
                        }

                        // Determine custom credit processor from configuration table
                        Globals.ConfigurationRecords = await Globals.client.GetConfigurationObjectsAsync(Globals.token, "KeyName='CreditProcessor'");
                        if (Globals.ConfigurationRecords.Count > 0)
                        {
                            string strCreditProcessor = Globals.ConfigurationRecords[0].col_Value.Trim().ToLower();
                            if (strCreditProcessor == "mercury")
                                Globals.processMercury = true;
                            else if (strCreditProcessor == "usaepay")
                            {
                                Globals.processUSAePay = true;
                                Globals.ConfigurationRecords = await Globals.client.GetConfigurationObjectsAsync(Globals.token, "KeyName='USAePaySourceKey'");
                                if (Globals.ConfigurationRecords.Count > 0)
                                    Globals.strUSAePaySourceKey = Globals.ConfigurationRecords[0].col_Value.Trim();
                                Globals.ConfigurationRecords = await Globals.client.GetConfigurationObjectsAsync(Globals.token, "KeyName='USAePayPin'");
                                if (Globals.ConfigurationRecords.Count > 0)
                                    Globals.strUSAePayPin = Globals.ConfigurationRecords[0].col_Value.Trim();
                            }
                            else if (strCreditProcessor == "blackline")
                            {
                                Globals.processBlackline = true;
                                Globals.ConfigurationRecords = await Globals.client.GetConfigurationObjectsAsync(Globals.token, "KeyName='BlacklineKey'");
                                if (Globals.ConfigurationRecords.Count > 0)
                                    Globals.strBlacklineKey = Globals.ConfigurationRecords[0].col_Value.Trim();
                                Globals.ConfigurationRecords = await Globals.client.GetConfigurationObjectsAsync(Globals.token, "KeyName='BlacklinePassword'");
                                if (Globals.ConfigurationRecords.Count > 0)
                                    Globals.strBlacklinePassword = Globals.ConfigurationRecords[0].col_Value.Trim();
                                Globals.ConfigurationRecords = await Globals.client.GetConfigurationObjectsAsync(Globals.token, "KeyName='BlacklineTerminalID'");
                                if (Globals.ConfigurationRecords.Count > 0)
                                    Globals.strBlacklineTerminalID = Globals.ConfigurationRecords[0].col_Value.Trim();
                                Globals.ConfigurationRecords = await Globals.client.GetConfigurationObjectsAsync(Globals.token, "KeyName='BlacklineUrl'");
                                if (Globals.ConfigurationRecords.Count > 0)
                                    Globals.strBlacklineUrl = Globals.ConfigurationRecords[0].col_Value.Trim();
                            }
                        }

                        // Determine custom credit processor service address from configuration table
                        Globals.ConfigurationRecords = await Globals.client.GetConfigurationObjectsAsync(Globals.token, "KeyName='CreditProcessorService'");
                        if (Globals.ConfigurationRecords.Count > 0)
                        {
                            string strCreditProcessorServiceAddress = Globals.ConfigurationRecords[0].col_Value.Trim();
                            if (strCreditProcessorServiceAddress.Length > 0)
                            {
                                if (Globals.processMercury)
                                {
                                    // Create a new instance for processing mercury payments
                                    Globals.mercuryClient = new ServiceReferenceMercury.MercuryClient();
                                    try
                                    {
                                        Globals.mercuryClient.Endpoint.Address = new EndpointAddress(new Uri(strCreditProcessorServiceAddress),
                                        Globals.mercuryClient.Endpoint.Address.Identity, Globals.client.Endpoint.Address.Headers.ToArray());
                                        await Globals.mercuryClient.OpenAsync();
                                    }
                                    catch (Exception)
                                    { }
                                    Globals.mercuryHostedCheckoutClient = new ServiceReferenceMercuryHostedCheckout.HCServiceSoapClient();
                                }
                                if (Globals.processUSAePay)
                                {
                                    // Create a new USAePay service
                                    Globals.usaepayClient = new ServiceReferenceUSAePay.ServiceUSAePayClient(ServiceReferenceUSAePay.ServiceUSAePayClient.EndpointConfiguration.BasicHttpsBinding_IServiceUSAePay, strCreditProcessorServiceAddress);
                                    await Globals.usaepayClient.OpenAsync();
                                }
                            }
                            // Determin if there's a credit card threshold...
                            Globals.ConfigurationRecords = await Globals.client.GetConfigurationObjectsAsync(Globals.token, "KeyName='CreditThreshold'");
                            if (Globals.ConfigurationRecords.Count > 0)
                            {
                                try
                                {
                                    Globals.credit_threshold = Convert.ToDouble(Globals.ConfigurationRecords[0].col_Value);
                                }
                                catch (Exception ex)
                                {
                                    System.Diagnostics.Debug.WriteLine(ex.Message);
                                }
                            }
                        }

                        Globals.initialized = true;
                    }
                    // Determine Time Clock Enable from local setting
                    if (Globals.timeclockbtnEnable == true && Globals.timeclockEnable == true)        //Show time clock dialog If click on time clock button
                    {
                            // Determine Time Clock Enable from local setting
                            Globals.RoleRecords = await Globals.client.GetRoleObjectsAsync(Globals.token, "Active=1 order by Title");
                            Globals.EmployeeRoleRecords = await Globals.client.GetEmployeeRoleObjectsAsync(Globals.token, "EmployeeID=" + Globals.currentLogin.col_ID + " and Active=1");
                            // Time Clock Dialog...
                            TimeClockDialog tcd = new TimeClockDialog();
                            tcd.Width = Windows.UI.Xaml.Window.Current.Bounds.Width;
                            tcd.Height = Windows.UI.Xaml.Window.Current.Bounds.Height;
                            tcd.CloseRequested += TimeClockDialog_CloseRequested;
                            dialogPopup = new Popup();
                            dialogPopup.Child = tcd;
                            dialogPopup.IsOpen = true;  
                    }
                    else if (Globals.chkTimeclockEnable == true && Globals.timeclockbtnEnable == true)      //  Show time clock dialog If PIN Enable
                    {
                        // Determine Time Clock Enable from local setting
                        Globals.RoleRecords = await Globals.client.GetRoleObjectsAsync(Globals.token, "Active=1 order by Title");
                        Globals.EmployeeRoleRecords = await Globals.client.GetEmployeeRoleObjectsAsync(Globals.token, "EmployeeID=" + Globals.currentLogin.col_ID + " and Active=1");
                        // Time Clock Dialog...
                        TimeClockDialog tcd = new TimeClockDialog();
                        tcd.Width = Windows.UI.Xaml.Window.Current.Bounds.Width;
                        tcd.Height = Windows.UI.Xaml.Window.Current.Bounds.Height;
                        tcd.CloseRequested += TimeClockDialog_CloseRequested;
                        dialogPopup = new Popup();
                        dialogPopup.Child = tcd;
                        dialogPopup.IsOpen = true; 
                    }
                    else
                    {
                        // Navigate to the main page...
                        Frame rootFrame = Window.Current.Content as Frame;
                        rootFrame.Navigate(typeof(MainPage));
                    }
                }
                else
                {
                    // Display error message
                    tblkErrorMessage.Text = "Invalid Login";
                }
            }
            catch (Exception ex)
            {
                tblkErrorMessage.Text = "Error: Unable to login";
#if DEBUG
                System.Diagnostics.Debug.WriteLine(ex.Message);
#endif
            }

            finally
            {
                // Restore the UI
                tbUsername.IsEnabled = true;
                pbPassword.IsEnabled = true;
                pbPin.IsEnabled = true;
                btnLogin.IsEnabled = true;
                btnTimeClock.IsEnabled = true;
                btnUserLogin.IsEnabled = true;
                btnLoginPIN.IsEnabled = true;
//                hbAccount.IsEnabled = true;
                gridLinks.Visibility = Visibility.Visible;
                imgLogo.Opacity = 1.0;
                tblkOpenRPOS.Opacity = 1.0;
                tblkUsername.Opacity = 1.0;
                tblkPassword.Opacity = 1.0;
                tblkPin.Opacity = 1.0;
                prWait.IsActive = false;
                pbPin.Password = "";
                pbPin.Focus(Windows.UI.Xaml.FocusState.Pointer);
            }
            
        }

        private void btnTimeClock_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // Disable the UI
                tbUsername.IsEnabled = false;
                pbPin.IsEnabled = false;
                pbPassword.IsEnabled = false;
                btnLogin.IsEnabled = false;
                btnTimeClock.IsEnabled = false;
                btnUserLogin.IsEnabled = false;
                btnLoginPIN.IsEnabled = false;
                imgLogo.Opacity = 0.5;
                tblkOpenRPOS.Opacity = 0.5;
                tblkUsername.Opacity = 0.5;
                tblkPassword.Opacity = 0.5;
                tblkPin.Opacity = 0.5;
                prWait.IsActive = true;
//                hbAccount.IsEnabled = false;
                gridLinks.Visibility = Visibility.Collapsed;
                Globals.timeclockEnable = true;
                Loginmethod();

            }
            catch (Exception ex)
            {
                tblkErrorMessage.Text = "Error: Unable to login";
#if DEBUG
                System.Diagnostics.Debug.WriteLine(ex.Message);
#endif
            }
        }


        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // Disable the UI
                tbUsername.IsEnabled = false;
                pbPin.IsEnabled = false;
                pbPassword.IsEnabled = false;
                btnLogin.IsEnabled = false;
                btnTimeClock.IsEnabled = false;
                btnUserLogin.IsEnabled = false;
                btnLoginPIN.IsEnabled = false;
                imgLogo.Opacity = 0.5;
                tblkOpenRPOS.Opacity = 0.5;
                tblkUsername.Opacity = 0.5;
                tblkPassword.Opacity = 0.5;
                tblkPin.Opacity = 0.5;
                prWait.IsActive = true;
//                hbAccount.IsEnabled = false;
                gridLinks.Visibility = Visibility.Collapsed;
                Globals.timeclockEnable = false;
                Loginmethod();
            }
            catch (Exception ex)
            {
                tblkErrorMessage.Text = "Error: Unable to login";
#if DEBUG
                System.Diagnostics.Debug.WriteLine(ex.Message);
#endif
            }
        }

        private void TimeClockDialog_CloseRequested(object sender, EventArgs e)
        {
            // Close the dialog...
            dialogPopup.IsOpen = false;
        }

        private void pbPin_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            if ((e.Key >= VirtualKey.Number0 && e.Key <= VirtualKey.Number9) || (e.Key >= VirtualKey.NumberPad0 && e.Key <= VirtualKey.NumberPad9) || (e.Key == VirtualKey.Tab))
                e.Handled = false;
            else e.Handled = true;
            // Attempt login when the "Enter" key is pressed in the pin box
            if (e.Key == VirtualKey.Enter)
            {
                if (Globals.chkTimeclockEnable == true)
                {
                    btnTimeClock_Click(sender, e);
                }
                else
                {
                    btnLogin_Click(sender, e);
                } 
            }
        }

        private void pbPassword_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            // Attempt login when the "Enter" key is pressed in the password box
            if (e.Key == VirtualKey.Enter)
            {
                if (Globals.chkTimeclockEnable == true)
                {
                    btnTimeClock_Click(sender, e);
                }
                else
                {
                    btnLogin_Click(sender, e);
                } 
            }
            
        }

        private void imgLogo_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
        {
            // Present dialog with web service address...
            ServiceAddressDialog sad = new ServiceAddressDialog();
            sad.Width = Windows.UI.Xaml.Window.Current.Bounds.Width;
            sad.Height = Windows.UI.Xaml.Window.Current.Bounds.Height;
            sad.CloseRequested += ServiceAddressDialog_CloseRequested;
            sad.init();
            dialogPopup = new Popup();
            dialogPopup.Child = sad;
            dialogPopup.IsOpen = true;
        }

        private void ServiceAddressDialog_CloseRequested(object sender, EventArgs e)
        {
            // Close the dialog...
            dialogPopup.IsOpen = false;
            // Update web service...
            ServiceAddressDialog sad = (ServiceAddressDialog)dialogPopup.Child;
            if (sad.bUpdated)
            {
                // Save current address as a local setting...
                Globals.AddOrUpdateValue(Globals.k_str_settings_service_address, sad.strServiceAddress);
                Globals.initialized = false;
                Init();
            }
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            // Determine if a parameter was passed from the command line
            if (Globals.InputURI != null && Globals.InputURI.ToString().ToLower().Contains(".svc"))
            {
                // Save current address as a local setting...
                Globals.AddOrUpdateValue(Globals.k_str_settings_service_address, "http" + Globals.InputURI.ToString().Substring(Globals.k_int_protocol_offset));
            }
        }


        //Code for numeric keypad functionality
        private void KeyPadControl_Click(object sender, RoutedEventArgs e)
        {
            if (NumericKeyPadControl.ClickedButtonValue == "Clear")
            {
                pbPin.Password = "";
            }
            else if (NumericKeyPadControl.ClickedButtonValue == "Enter")
            {
                if (Globals.chkTimeclockEnable == true)
                {
                    btnTimeClock_Click(sender, e);
                }
                else
                {
                    btnLogin_Click(sender, e);
                } 
            }
            else if (NumericKeyPadControl.ClickedButtonValue == "Back")
            {
                if (pbPin.Password.Length > 0)
                {
                    pbPin.Password = pbPin.Password.Remove(pbPin.Password.Length - 1, 1);
                }
            }
            else if (NumericKeyPadControl.ClickedButtonValue != ".")
            {
                if (pbPin.Password.Length < 4)
                {
                    pbPin.Password += NumericKeyPadControl.ClickedButtonValue;
                }
            }
        }

        private void pbPin_GotFocus(object sender, RoutedEventArgs e)
        {
            NumericKeyPadControl.Visibility = Visibility.Visible;
        }

        private void pbPin_Changed(object sender, RoutedEventArgs e)
        {
            pbPin.Focus(Windows.UI.Xaml.FocusState.Pointer);

            if (pbPin.Password.Length >= 4)
            {
                if (Globals.chkTimeclockEnable == true)
                {
                    btnTimeClock_Click(sender, e); 
                }
                else
                {
                    btnLogin_Click(sender, e); 
                } 
            }
        }

        private void chkTimeClock_Checked(object sender, RoutedEventArgs e)
        {
            if (chkTimeClock.IsChecked == true)
            {
                Globals.chkTimeclockEnable = true;
            }
            else
            {
                Globals.chkTimeclockEnable = false;
            }
        }
    }
}
